﻿#include "stdafx.h"
#include "W_VoltageMonitor.h"

#include "Source/MonitorModule/S_MonitorDataContainer.h"
#include "Source/MonitorModule/S_MonitorDisplayingManager.h"
#include "Source/ResultModule/S_ResultWindowManager.h"
#include "Source/ModelingPreModule/S_ModelingPre_InteractiveManager.h"

#include "Source/MainModule/Global/S_FileDefaultDirectory.h"
#include "Source/MainModule/Global/S_TabOrderManager.h"
#include "Source/Utils/Common/TTool.h"

/*
-----==========================================================-----
		类：		电压监视器 编辑窗口.cpp
		作者：		drill_up
		所属模块：	监视器模块
		功能：		电压监视器数据类的编辑窗口。
-----==========================================================-----
*/
W_VoltageMonitor::W_VoltageMonitor(QWidget *parent)
	: W_DialogBase(parent)
{
	ui.setupUi(this);


	//-----------------------------------
	//----参数初始化
	this->m_slotBlock_model = false;


	//-----------------------------------
	//----ui初始化

	// > 窗口初始化
	this->setDataInAddMode();
	this->initDialogButtonBox(ui.buttonBox);


	//-----------------------------------
	//----事件绑定
	connect(ui.lineEdit_x1, &QLineEdit::textEdited, this, &W_VoltageMonitor::refreshModel);
	connect(ui.lineEdit_y1, &QLineEdit::textEdited, this, &W_VoltageMonitor::refreshModel);
	connect(ui.lineEdit_z1, &QLineEdit::textEdited, this, &W_VoltageMonitor::refreshModel);
	connect(ui.lineEdit_x2, &QLineEdit::textEdited, this, &W_VoltageMonitor::refreshModel);
	connect(ui.lineEdit_y2, &QLineEdit::textEdited, this, &W_VoltageMonitor::refreshModel);
	connect(ui.lineEdit_z2, &QLineEdit::textEdited, this, &W_VoltageMonitor::refreshModel);
	connect(ui.checkBox_invertDirection, &QCheckBox::toggled, this, &W_VoltageMonitor::refreshModel);
		
	//-----------------------------------
	//----配置tab顺序
	S_TabOrderManager::getInstance()->configureTabOrder(this);
}

W_VoltageMonitor::~W_VoltageMonitor(){
}

Ui::W_VoltageMonitor *W_VoltageMonitor::getUI()
{
	return &ui;
}

/*-------------------------------------------------
		控件 - 获取自动命名
*/
QString W_VoltageMonitor::getAutoName() {
	QString result_name;
	int temp_id = 1;
	while (true) {
		result_name = tr("VoltageMonitor") + QString::number(temp_id);
		if (S_MonitorDataContainer::getInstance()->hasName(result_name) == false) {
			break;
		}else {
			temp_id++; continue;
		}
	}
	return result_name;
}


/*-------------------------------------------------
		模型 - 刷新模型
*/
void W_VoltageMonitor::refreshModel() {
	if (this->m_slotBlock_model == true) { return; }

	// > 若已存在，则删除
	if (this->m_model_id != "") {
		S_ModelingPre_InteractiveManager::getInstance()->model_destory(this->m_model_id);
	}

	// > 临时创建
	double x1 = ui.lineEdit_x1->text().toDouble();
	double y1 = ui.lineEdit_y1->text().toDouble();
	double z1 = ui.lineEdit_z1->text().toDouble();
	double x2 = ui.lineEdit_x2->text().toDouble();
	double y2 = ui.lineEdit_y2->text().toDouble();
	double z2 = ui.lineEdit_z2->text().toDouble();
	bool invertDirection = ui.checkBox_invertDirection->isChecked();
	if (invertDirection == false) {
		this->m_model_id = S_ModelingPre_InteractiveManager::getInstance()->model_CreateDiscretePort(
			gp_Pnt(x1, y1, z1), gp_Pnt(x2, y2, z2),
			QColor(120, 120, 120), QColor(0, 0, 255)
		);
	}else {
		this->m_model_id = S_ModelingPre_InteractiveManager::getInstance()->model_CreateDiscretePort(
			gp_Pnt(x2, y2, z2), gp_Pnt(x1, y1, z1),
			QColor(120, 120, 120), QColor(0, 0, 255)
		);
	}

}
/*-------------------------------------------------
		模型 - 删除模型
*/
void W_VoltageMonitor::clearModel() {
	S_ModelingPre_InteractiveManager::getInstance()->model_destory(this->m_model_id);
}


/*-------------------------------------------------
		窗口 - 设置两点的数据
*/
void W_VoltageMonitor::setDataPoint(QList<double> p1, QList<double> p2) {
	if (p1.count() < 3) { return; }
	ui.lineEdit_x1->setText(QString::number(p1.at(0)));
	ui.lineEdit_y1->setText(QString::number(p1.at(1)));
	ui.lineEdit_z1->setText(QString::number(p1.at(2)));
	ui.checkBox_usePick->setChecked(true);

	if (p2.count() < 3) { return; }
	ui.lineEdit_x2->setText(QString::number(p2.at(0)));
	ui.lineEdit_y2->setText(QString::number(p2.at(1)));
	ui.lineEdit_z2->setText(QString::number(p2.at(2)));
	ui.checkBox_usePick_2->setChecked(true);

	// > 刷新模型
	this->refreshModel();
}
/*-------------------------------------------------
		窗口 - 设置数据（添加）
*/
void W_VoltageMonitor::setDataInAddMode() {
	this->local_VoltageMonitor.init();
	this->isAddMode = true;
	this->putDataToUi();

	// > 自动命名
	ui.lineEdit_name->setText(this->getAutoName());
}
/*-------------------------------------------------
		窗口 - 设置数据（修改）
*/
void W_VoltageMonitor::setDataInModifyMode(C_VoltageMonitor p) {
	this->local_VoltageMonitor = p;
	this->isAddMode = false;
	this->putDataToUi();
}

/*-------------------------------------------------
		窗口 - 取出数据
*/
C_VoltageMonitor W_VoltageMonitor::getData(){
	return this->local_VoltageMonitor;
};
/*-------------------------------------------------
		窗口 - 本地数据 -> ui数据
*/
void W_VoltageMonitor::putDataToUi() {
	this->m_slotBlock_model = true;

	ui.lineEdit_name->setText(this->local_VoltageMonitor.getName());

	TTool::_double_(ui.lineEdit_x1, &this->local_VoltageMonitor.x1);
	TTool::_double_(ui.lineEdit_y1, &this->local_VoltageMonitor.y1);
	TTool::_double_(ui.lineEdit_z1, &this->local_VoltageMonitor.z1);
	TTool::_double_(ui.lineEdit_x2, &this->local_VoltageMonitor.x2);
	TTool::_double_(ui.lineEdit_y2, &this->local_VoltageMonitor.y2);
	TTool::_double_(ui.lineEdit_z2, &this->local_VoltageMonitor.z2);
	ui.checkBox_invertDirection->setChecked(this->local_VoltageMonitor.directionInverted);

	this->m_slotBlock_model = false;
	this->refreshModel();
};
/*-------------------------------------------------
		窗口 - ui数据 -> 本地数据
*/
void W_VoltageMonitor::putUiToData() {

	this->local_VoltageMonitor.setName(ui.lineEdit_name->text());

	TTool::_double_(&this->local_VoltageMonitor.x1, ui.lineEdit_x1);
	TTool::_double_(&this->local_VoltageMonitor.y1, ui.lineEdit_y1);
	TTool::_double_(&this->local_VoltageMonitor.z1, ui.lineEdit_z1);
	TTool::_double_(&this->local_VoltageMonitor.x2, ui.lineEdit_x2);
	TTool::_double_(&this->local_VoltageMonitor.y2, ui.lineEdit_y2);
	TTool::_double_(&this->local_VoltageMonitor.z2, ui.lineEdit_z2);
	this->local_VoltageMonitor.directionInverted = ui.checkBox_invertDirection->isChecked();
};
/*-------------------------------------------------
		窗口 - 提交数据（校验）
*/
void W_VoltageMonitor::base_acceptData(){
	this->putUiToData();

	// > 校验 - 名称
	QString name = this->local_VoltageMonitor.getName();
	if ( name == "" ){
		QMessageBox::warning(this, tr("提示"), tr("名称不能为空。"));
		return;
	}
	if (this->isAddMode == true && S_MonitorDataContainer::getInstance()->hasName(name)) {
		QMessageBox::warning(this, tr("提示"), tr("已有此名称，不可重复。"));
		return;
	}

	// > 校验 - 长度
	if (this->local_VoltageMonitor.x1 == this->local_VoltageMonitor.x2 &&
		this->local_VoltageMonitor.y1 == this->local_VoltageMonitor.y2 &&
		this->local_VoltageMonitor.z1 == this->local_VoltageMonitor.z2) {
		QMessageBox::warning(this, tr("提示"), tr("电压监视器的两点长度不能为零。"));
		return;
	}

	// > 校验 - 操作是否与结果文件冲突（含迁移处理）
	if (S_ResultWindowManager::getInstance()->isConflictToResult_WithMigrateTip()) {
		return;
	}

	emit acceptDataSignal(this->isAddMode, this->local_VoltageMonitor);
	W_DialogBase::base_acceptData();

	// > 清理模型
	this->clearModel();
};
/*-------------------------------------------------
		窗口 - 取消数据（校验）
*/
void W_VoltageMonitor::base_rejectData() {
	W_DialogBase::base_rejectData();

	// > 清理模型
	this->clearModel();

	// > 重新显示旧模型
	if (this->isAddMode == false) {
		S_MonitorDisplayingManager::getInstance()->refreshModel_NoUpdate_VoltageMonitor(this->local_VoltageMonitor.getId());
		S_ModelingPre_InteractiveManager::getInstance()->updateViewModeling();
	}
}
/*-------------------------------------------------
		窗口 - 关闭事件
*/
void W_VoltageMonitor::closeEvent(QCloseEvent *e) {
	W_DialogBase::closeEvent(e);
	
	// > 清理模型
	this->clearModel();
}